package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dao.AddressDao;
import dao.RowMapper;
import entity.Address;
import entity.Area;
import entity.Customer;
import entity.Street;

public class AddressDaoImpl implements AddressDao {
	private JDBCTemplate jdbc = new JDBCTemplate();

	private class RowMapperImpl implements RowMapper<Address>{

		@Override
		public Address rowMapper(ResultSet rs) throws SQLException {
			Customer customer = new Customer();
			customer.setCusId(rs.getInt("cusId"));
			customer.setEmail(rs.getString("email"));
			customer.setPassword(rs.getString("password"));
			customer.setPhone(rs.getString("phone"));
			customer.setRegisterDate(rs.getDate("registerDate"));
			customer.setUserName(rs.getString("userName"));
			
			Street street = new Street(rs.getInt("strId"), new Area(rs.getInt("aId"), rs.getString("areaName"), null), rs.getString("streetName"));
			Address address = new Address(rs.getInt("addrid"), customer, street, rs.getString("detailAddress"));
			String compAddr=jdbc.queryCompleteAddress(address);
			address.setCompleteAddress(compAddr);
			return address;
		}
	}
	
	
	@Override
	public List<Address> queryAllAddress() {
		String sql = "SELECT address.*,userName,`password`,"+
					" phone,registerDate,email,"+
					" `area`.areaId AS aId,streetName,areaName"+
					" FROM address"+
					" INNER JOIN customer ON customer.`cusId` = address.`cusId`"+
					" INNER JOIN street ON street.`strId` = address.`strId`"+
					" INNER JOIN `area` ON `area`.`areaId`=street.`areaId`";
		return jdbc.queryAll(sql, new RowMapperImpl());
	}
	

	@Override
	public Address queryAddressByAddressId(int AddressId) {
		String sql = "SELECT address.*,userName,`password`,"+
				" phone,registerDate,email,"+
				" `area`.areaId AS aId,streetName,areaName"+
				" FROM address"+
				" INNER JOIN customer ON customer.`cusId` = address.`cusId`"+
				" INNER JOIN street ON street.`strId` = address.`strId`"+
				" INNER JOIN `area` ON `area`.`areaId`=street.`areaId`"+
				" WHERE addrId=?";
		return jdbc.queryOne(sql, new RowMapperImpl(), AddressId);
	}

	@Override
	public List<Address> queryAddressByCusId(int customerId) {
		String sql = "SELECT address.*,userName,`password`,"+
				" phone,registerDate,email,"+
				" `area`.areaId AS aId,streetName,areaName"+
				" FROM address"+
				" INNER JOIN customer ON customer.`cusId` = address.`cusId`"+
				" INNER JOIN street ON street.`strId` = address.`strId`"+
				" INNER JOIN `area` ON `area`.`areaId`=street.`areaId`"+
				" WHERE address.cusId=?"+
				" Order by addrId";
		return jdbc.queryAll(sql, new RowMapperImpl(), customerId);
	}


	@Override
	public void addAddress(Address address) {
		String sql = "INSERT INTO address(cusId,strId,detailAddress) VALUES(?,?,?)";
		jdbc.CUDObject(sql, address.getCustomer().getCusId(),address.getStreet().getStrId(),address.getDetailAddress());
		
	}

}
